package com.example.demo.controller;

import com.example.demo.entity.ResultEntity;
import com.example.demo.service.serviceimpl.HistoryServiceImpl;
import com.example.demo.vo.HistoryVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/history/")
@CrossOrigin
@RestController
public class HistoryController {
    @Resource
    HistoryServiceImpl historyService;

    // 获取用户浏览历史
    @GetMapping("getUserHistory")
    private ResultEntity getUserHistory(@RequestParam("user_id") Integer user_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(historyService.getUserHistory(user_id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取历史记录失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 添加浏览历史（如果已存在则更新）
    @PostMapping("addHistory")
    private ResultEntity addHistory(@RequestBody HistoryVo historyVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if(historyService.updateHistory(historyVo) > 0){
                resultEntity.setCode(0);
                resultEntity.setMsg("更新历史记录成功");
            }else{
                if (historyService.addHistory(historyVo) > 0) {
                    resultEntity.setCode(0);
                    resultEntity.setMsg("添加历史记录成功");
                } else {
                    resultEntity.setCode(-1);
                    resultEntity.setMsg("添加历史记录失败");
                }
            }
            
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 删除浏览历史
    @DeleteMapping("deleteHistoryById")
    private ResultEntity deleteHistoryById(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (historyService.deleteHistory(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据用户ID和商品ID删除浏览历史
    @DeleteMapping("deleteHistoryByUserAndProduct")
    private ResultEntity deleteHistoryByUserAndProduct(@RequestParam("user_id") Integer user_id, @RequestParam("products_id") Integer products_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (historyService.deleteHistoryByUserAndProduct(user_id, products_id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 更新浏览历史
    @PutMapping("updateHistory")
    private ResultEntity updateHistory(@RequestBody HistoryVo historyVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (historyService.updateHistory(historyVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("更新成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("更新失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    @GetMapping("getHistoryById")
    private ResultEntity getHistoryById(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(historyService.getHistoryById(id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取历史记录失败");
            e.printStackTrace();
        }
        return resultEntity;
    }
}